﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace TMIS
{
    public partial class frmRegStudent : XtraForm
    {
        private BLL.Student bll = new BLL.Student();
        public frmRegStudent()
        {
            InitializeComponent();
            this.FillComponent();
        }
        private void FillComponent()
        {
            comDepartment.Properties.Items.Clear();
            comDepartment.Properties.Items.AddRange(bll.GetDepartment());
            comTeacher.Properties.Items.Clear();
            comTeacher.Properties.Items.AddRange(bll.GetTheacher());
            comThesis.Properties.Items.Clear();
            comThesis.Properties.Items.AddRange(bll.GetThesis());
        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (bll.Check(txtName.Text, txtPassword.Text)==false)
            {
                Model.Student stu = new Model.Student()
                {
                    ID = Guid.NewGuid(),
                    sName = txtName.Text,
                    sPassword = txtPassword.Text,
                    sNumber = txtNumber.Text,
                    Sex = comSex.Text,
                    DepartmentID = bll.GetDepartmentID(comDepartment.Text),
                    ProfessionID = bll.GetProfessionID(comProfession.Text),
                    GradeID = bll.GetGradeID(comGrade.Text),
                    ClassID = bll.GetClassID(comClass.Text),
                    TeacherID = bll.GetTeacherID(comTeacher.Text),
                    ThesisID = bll.GetThesisID(comThesis.Text)
                };
                try
                {
                    bll.Save(stu);
                    XtraMessageBox.Show("学生信息添加成功！");
                }
                catch (Exception error)
                {
                    #if DEBUG
                    XtraMessageBox.Show(error.Message);
                    #else 
                    XtraMessageBox.Show("信息保存失败！请检查所有信息是否填写完整！");
                    #endif
                }
            }
            else
            {
                XtraMessageBox.Show("学生信息已经存在，信息添加失败！");
            }
        }

        private void comDepartment_SelectedValueChanged(object sender, EventArgs e)
        {
            comProfession.Properties.Items.Clear();
            comProfession.Properties.Items.AddRange(bll.GetProfession(comDepartment.Text));
        }

        private void comProfession_SelectedValueChanged(object sender, EventArgs e)
        {
            comGrade.Properties.Items.Clear();
            comGrade.Properties.Items.AddRange(bll.GetGrade(comProfession.Text));
        }

        private void comGrade_SelectedValueChanged(object sender, EventArgs e)
        {
            comClass.Properties.Items.Clear();
            comClass.Properties.Items.AddRange(bll.GetClass(comGrade.Text));
        }
    }
}
